package com.example.newinfinity.MyViewModel

import android.app.Application
import android.content.Context
import android.graphics.Color
import android.graphics.Paint.Style
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.example.newinfinity.View.Layer.LayerManager

//提供给绘制功能的viewModel
//内部提供单例 确保不能获取context的类中也可以使用该viewModel
class DrawViewModel(application: Application) : AndroidViewModel(application) {

    //提供绘制图形的画笔的参数 令外部修改保存统一性
    var mStrokeWidth: Float = 20f
    var mColor = Color.BLACK
    var mStyle = Style.FILL
    //专门提供给橡皮擦的宽度
    var eraserStrokeWidth:Float = 40f

    //存放LayerManager 供DrawView获取并调用其中的方法
    val layerManager = LayerManager()

    //给外部提供一个context
    fun getContext(): Context {
        return getApplication()
    }

    //initDrawViewModel中创建自己的对象
    //getDrawViewModel中让外部获取自己对象的实例
    companion object {
        private var instance: DrawViewModel? = null

        //使用该viewModel之前需要初始化
        fun initDrawViewModel(owner: ViewModelStoreOwner) {
            if (instance == null) {
                instance = ViewModelProvider(owner).get(DrawViewModel::class.java)
            }
        }

        //获取单例
        fun getDrawViewModel(): DrawViewModel {
            if (instance == null) {
                throw Exception("请先初始化")
            }
            return instance!!
        }
    }


}